<?php
/**
 * Created by PhpStorm.
 * User: zcq
 * Date: 2019/9/6
 * Time: 10:24
 */

namespace app\admin\controller;


use think\Db;

class Term extends Base
{
    public $table = 'term';

    public function index()
    {
        $type = input('type');
        $keyword = input('keyword');
        $where = null;
        if($type){
            $where['type'] = ['eq',$type];
        }
        if($keyword){
            $where['name'] = ['like','%'.$keyword.'%'];
        }
        if(!$where){
            $where = '1=1';
        }
        $data['list'] = Db::name($this->table)
            ->where('site_id',$this->site_id)
            ->where($where)
            ->order('sort desc,id asc')
            ->select();

        $data['list'] = $this->tree($data['list']);
        $this->getLevelOneTerm(true);
        $this->assign('data',$data);
        $this->assign('type',$type);
        $this->assign('keyword',$keyword);
        return $this->fetch();
    }

    public function tree($data,$pid = 0)
    {
        static $new = [];
        if($data){
            foreach ($data as $value){
                if($value['pid'] == $pid){
                    $new[] = $value;
                    $this->tree($data,$value['id']);
                }
            }
        }
        return $new;
    }

    public function add()
    {
        if(request()->isPost()){
            return $this->save();
        }
        $this->getLevelOneTerm(false);
        $this->assign('data',1);
        $this->assign('title','新增');
        $this->assign('url',url('add'));
        return $this->fetch('info');
    }

    public function update()
    {
        $id = input('id/d');
        $data = Db::name($this->table)->where('id',$id)->find();
        if(!$data){
            $this->error('不存在');
        }
        if(request()->isPost()){
            return $this->save();
        }
        $this->getLevelOneTerm(true);

        $this->assign('data',json_encode($data));
        $this->assign('title','编辑');
        $this->assign('url',url('update'));
        return $this->fetch('info');
    }

    public function save()
    {
        $id = input('id/d');
        $data = [
            'name'=>input('name'),
            'type'=>input('type'),
            'link'=>input('link'),
            'pid'=>input('pid'),
            'sort'=>input('sort'),
            'modtime'=>time(),
        ];
        if($id){
            $a = Db::name($this->table)->where('id',$id)->update($data);
        }else{
            $data['addtime'] = time();
            $data['level'] = $data['pid']?2:1;
            $data['site_id'] = $this->site_id;
            $data['type'] = $data['pid']?4:$data['type'];
            $a = Db::name($this->table)->insert($data);
        }
        if($a){
            echo api_return(1,'保存成功');
        }else{
            echo api_return(0,'保存失败');
        }
        return;
    }

    public function getLevelOneTerm($all = true)
    {
        $data = Db::name($this->table)
            ->where('site_id',$this->site_id)
            ->where('level',1)
            ->where(function ($query)use ($all){
                if(!$all){
                    $query->where('type',1);
                }
            })
            ->order('sort desc,id desc')->select();
        $this->assign('config_json',json_encode($data,JSON_UNESCAPED_UNICODE));
        $this->assign('config_arr',$data);
        return $data;
    }

}